package com.zhuanzhuan.hero.user.converter;

import com.zhuanzhuan.hero.member.enums.MemberStatusEnum;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.entity.HeroUserContact;
import com.zhuanzhuan.hero.user.entity.HeroUserInfo;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.enums.UserStateEnum;
import com.zhuanzhuan.hero.user.service.IApolloService;
import com.zhuanzhuan.hero.user.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName UserInfoConvert
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/9 17:05
 */
@Component
@Slf4j
public class UserInfoConvert {

    private static final String SPECIAL_CITY = "深圳";

    private static final String UNREGISTERED_NICKNAME = "用户已注销";

    private static final String MY_RECYCLE_ORDER_FORMAT = "我的回收单";

    private static final String MY_SELL_FORMAT = "我卖的";

    private static final String BRAND_REPLACE_PREFIX = "优势";

    @Resource
    private IApolloService apolloService;

    /**
     * 用户昵称  区分已注销的情况
     *
     * @param heroUserInfo
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/9
     **/
    public String getNicknameWithUnregist(HeroUserInfo heroUserInfo) {
        return heroUserInfo.isUnregistered() ? UNREGISTERED_NICKNAME : heroUserInfo.getNickname();
    }

    public String getNicknameWithUnregist(HeroUserInfo heroUserInfo, String nickname) {
        return heroUserInfo.isUnregistered() ? UNREGISTERED_NICKNAME : nickname;
    }

    /**
     * 用户头像  区分已注销的情况
     *
     * @param heroUserInfo
     * @param defaultProfilePhoto 可以为空，为空则查apollo
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/9
     **/
    public String getProfilePhotoWithUnregist(HeroUserInfo heroUserInfo, String defaultProfilePhoto) {
        if (!heroUserInfo.isUnregistered()) {
            return heroUserInfo.getProfilePhoto();
        }
        return StringUtils.isBlank(defaultProfilePhoto) ?
            apolloService.queryDefauleProperties().getDefaultProfilePhoto() : defaultProfilePhoto;
    }

    public String getOuterNicknameWithUnregist(HeroUserInfo heroUserInfo, HeroUserContact userContact) {
        return heroUserInfo.isUnregistered() ? UNREGISTERED_NICKNAME : userContact.getOuterNickname();
    }

    public String getOuterProfilePhotoWithUnregist(HeroUserInfo heroUserInfo, HeroUserContact userContact, String defaultProfilePhoto) {
        return heroUserInfo.isUnregistered() ? defaultProfilePhoto : userContact.getOuterProfilePhoto();
    }

    public String getUserCommentsWithUnregist(HeroUserInfo heroUserInfo, String userComments) {
        if (StringUtils.isBlank(userComments)) {
            return StringUtils.EMPTY;
        }
        return heroUserInfo.isUnregistered() ? UNREGISTERED_NICKNAME : userComments;
    }


    /**
     * 格式化【地区】展示
     *   展示省份特殊展示深圳
     * @param userContact
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/20
     **/
    public static String parseRegion(HeroUserContact userContact) {
        Assert.notNull(userContact, ExceptionMsgEnum.PARAM_ERROR);
        if (SPECIAL_CITY.equals(userContact.getStoreCity())) {
            return userContact.getStoreCity();
        }
        return userContact.getStoreProvince();
    }

    /**
     * 我的回收单  文案
     *
     * @param count
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/20
     **/
    public static String buildMyRecycleOrderTab(Integer count) {
        return buildTabContent(MY_RECYCLE_ORDER_FORMAT, count);
    }

    /**
     * 我卖的  文案
     *
     * @param count
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/20
     **/
    public static String buildMySellTab(Integer count) {
        return buildTabContent(MY_SELL_FORMAT, count);
    }

    /**
     * 优势介绍标签名展示
     *  将"优势品牌"处理成"品牌"
     *
     * @param labelName
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/20
     **/
    public static String convertAdvantageShowName(String labelName) {
        if (StringUtils.isBlank(labelName)) {
            return StringUtils.EMPTY;
        }
        if (labelName.startsWith(BRAND_REPLACE_PREFIX)) {
            return labelName.substring(NumConsts.TWO);
        }
        return labelName;
    }

    /**
     * 拼接案   >99的处理成99+
     *
     * @param count
     * @return java.lang.String
     * @author  hanjunjun
     * @date 2024/7/20
     **/
    private static String buildTabContent(String format, Integer count) {
        if (Objects.isNull(count) || count.compareTo(NumConsts.ZERO) <= 0) {
            return format;
        }
        //>99的处理成99+
        String numStr = count.compareTo(NumConsts.NINTY_NINE) > 0 ? "99+" : String.valueOf(count);
        return format + "(" + numStr + ")";
    }

    public static UserStateEnum mapUserStateByMemberStatus(Integer memberStatus) {
        if (MemberStatusEnum.ACTIVE.getStatus().equals(memberStatus)) {
            return UserStateEnum.SETTLED;
        }
        if (MemberStatusEnum.EXPIRED.getStatus().equals(memberStatus)) {
            return UserStateEnum.EXPIRED;
        }
        if (MemberStatusEnum.EXIT.getStatus().equals(memberStatus)) {
            return UserStateEnum.EXITED;
        }
        if (MemberStatusEnum.RETIRE.getStatus().equals(memberStatus)) {
            return UserStateEnum.EXPELLED;
        }
        return UserStateEnum.WAIT_SETTLE;
    }

    public static void main(String []args){
        System.out.println(UserInfoConvert.buildMySellTab(318));
    }
}
